package main.java.org.controller.user;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;







import main.java.org.common.WebConstant;
import main.java.org.dto.Organization;
import main.java.org.dto.User;
import main.java.org.dto.UserData;
import main.java.org.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserController {
    
	@Autowired
	private UserService userService;
	/**
	 * 跳转到登录界面
	 * @return
	 */
	@RequestMapping("toLogin")
	public String toLogin(){
		return "user/login";
	}
	/**
	 * 跳转到注册页面
	 * @return
	 */
	@RequestMapping("toRegister")
	public String toRegister(){
		return "user/register";
	}
	/**
	 * 注册操作
	 * @return
	 */
	@RequestMapping("register")
	public String register(User user,UserData userData,Organization org,HttpServletRequest request){
		if(null != org && !"".equals(org.getOrgName())){
			if(null != user && !"".equals(user.getUserName())){
				Map map = userService.register(org,user,userData);
				
				if(map != null){
					user = (User)map.get(WebConstant.USER);
					org = (Organization)map.get(WebConstant.ORG);
					request.setAttribute(WebConstant.USER, user);
					request.setAttribute(WebConstant.ORG, org);
					request.setAttribute(WebConstant.ORG, org);
					request.setAttribute(WebConstant.MSG, "恭喜您注册成功！");
					return "user/register_message";
				}else{
					request.setAttribute(WebConstant.MSG, "注册失败！");
				}
			}
		}
		return "user/register";
	}
	
	
	@RequestMapping("login")
	public String login() {
		System.out.println("--login----");
		User user = new User();
		user.setCreateTime(new Date());
		user.setUserName("admin");
		user.setUserPassword("12345678");
		userService.save(user);
		return "success";
	}
}
